﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using TfsWorkItemTracking = Microsoft.TeamFoundation.WorkItemTracking.Client;
using FunctionPoint.Utility;
namespace FunctionPoint.Common
{
    [Serializable]
    public class WorkItemQuery : QueryItemBase, INotifyPropertyChanged, IComparable<WorkItemQuery>, IComparable<QueryItemBase>
    {

        private bool isLoaded = false;

        public string Name { get; set; }
        public string ItemPath { get; set; }
        public Guid QueryId { get; set; }
        public WorkItemQueryFolder ParentFolder { get; set; }
        public ObservableCollection<WorkItem> WorkItems { get; set; }
        public bool IsNeedingRefresh { get; set; }

        public bool IsLoaded
        {
            get { return isLoaded; }
        }

        public int NestLevel { get; set; }

        public WorkItemQuery()
        {
            WorkItems = new ObservableCollection<WorkItem>();
            WorkItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(WorkItems_CollectionChanged);
        }

        void WorkItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("AllFoldersAndQueries"));

            isLoaded = true;
        }

        public override string ToString()
        {
            return ItemPath;
        }



        ObservableCollection<object> _allFoldersAndQueries = null;
        public ObservableCollection<object> AllFoldersAndQueries
        {
            get
            {

                _allFoldersAndQueries = new ObservableCollection<object>();
                _allFoldersAndQueries.AddRange(WorkItems);


                return _allFoldersAndQueries;

            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        public override QueryItemType ItemType
        {
            get { return QueryItemType.Query; }
        }

        public int CompareTo(QueryItemBase other)
        {
            var query = other as WorkItemQuery;
            
            if (query != null)
                return Convert.ToInt32(query != null && query.QueryId == this.QueryId);
            
            
            return -1;
        }

        public int CompareTo(WorkItemQuery other)
        {
            return Convert.ToInt32(other.QueryId == this.QueryId);
        }
    }
}
